import Vue from 'vue'
import moment from 'moment'
import 'moment/locale/zh-cn'
moment.locale('zh-cn')

//日期格式化 YYYY-MM-DD HH:mm:ss
Vue.filter('dateFormat', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
});

//校验数据有效性
Vue.filter('checkViliable', function (dataStr) {
  if(typeof dataStr === 'boolean') return dataStr;
  if(dataStr === undefined || dataStr === 'undefined' || dataStr == 'null' || dataStr === '') return '--';
  const checkVal = isNaN(Number(dataStr));
  if(!checkVal){
    if(Number(dataStr) || Number(dataStr) == 0){
      return Number(dataStr)
    }else{
      return '--'
    }
  }else{
    return dataStr
  }
});

//数字转换千分位，保留小数
Vue.filter('NumberFormat', function (value) {
  if(typeof value !== 'number') return value;
  if (!value) {
      return '0'
  }
  const intPartFormat = value.toString().replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,') 
  return intPartFormat
})